/** @namespace app.model.DetailAnchor */
module.exports = app => {
  const {STRING, INTEGER, DATE, JSON, BOOLEAN} = app.Sequelize;

  const DetailAnchor = app.model.define('detail_anchor', {
    name: {type: STRING},
    _id: {type: STRING, unique: 'anchor_and_platform'},
    url: STRING,
    gender: {type: BOOLEAN, defaultValue: true},
    // options: {type: JSON, defaultValue: {categories: []}},
    delete: {type: BOOLEAN, defaultValue: false},
  }, {
    timestamps: false,
    defaultScope: {
      where: {delete: false},
    },
    scopes: {deleted: {where: {delete: true}}},
  });

  DetailAnchor.associate = () => {
    const {
      DetailAnchor, DetailAnchorSchedule, DetailAnchorLevel,
      DetailAnchorData, DetailAnchorOption, Platform,
    } = app.model;
    DetailAnchor.hasMany(DetailAnchorData,
        {as: 'data', foreignKey: {name: 'anchor_id'}});
    DetailAnchor.hasMany(DetailAnchorSchedule, {
      as: 'schedule',
      foreignKey: {name: 'anchor_id'},
    });

    DetailAnchor.belongsTo(Platform);

    DetailAnchor.belongsTo(DetailAnchorLevel,
        {as: 'level', foreignKey: 'level_id'});
    DetailAnchorLevel.hasMany(DetailAnchor,
        {as: 'anchor', foreignKey: 'level_id'});

    DetailAnchor.hasOne(DetailAnchorOption,
        {as: 'option', foreignKey: 'anchor_id', constraints: false});

  };
  return DetailAnchor;
};